#pragma once

#include <string>
#include <vector>
#include <cstdint>

extern "C" typedef struct sqlite3 sqlite3;

enum CacheType: size_t {
    PSVGame,
    PSVDLC,
    PSVTheme,
    CacheTypeMax,
};

enum class Region: uint8_t {
    JP,
    US,
    EU,
    ASIA,
    INT,
    UNKNOWN,
};

struct PkgInfo {
    std::string titleId;
    Region region;
    std::string name;
    std::string link;
    std::string zRIF;
    std::string contentId;
    uint64_t lastUpdate;
    std::string originalName;
    uint64_t fileSize;
    std::string sha256;
    std::string fwVersion;
};

class DataCache {
public:
    DataCache();
    ~DataCache();

    inline uint64_t lastUpdate(CacheType t) { return lastUpdate_[t]; }
    inline uint64_t cacheTime(CacheType t) { return cacheTime_[t]; }
    void loadCache(CacheType type, const std::string &text, uint64_t updateTime);
    bool getPkgList(CacheType type, std::vector<PkgInfo> &list);
    static std::string regionToString(Region r);

private:
    void initInternal();
    void freeInternal();
    bool createOrReadConfig();
    void checkCache();

private:
    std::string baseDir_;
    std::string configFile_;
    std::string cacheFile_;
    sqlite3 *db_;
    uint64_t lastUpdate_[CacheTypeMax] {};
    uint64_t cacheTime_[CacheTypeMax] {};
};
